package com.auxgroup.hr.front.site.co.hireperson;

import javax.annotation.Resource;

import com.auxgroup.hr.front.site.vo.person.HirePhotoRespVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.config.BusinessConfig;
import com.auxgroup.hr.front.commons.utils.File2Byte;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.dao.domain.PsRsHirePhoto;
import com.auxgroup.hr.front.dao.domain.PsRsHirePhotoPrimary;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.hireperson.HirePhotoService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.vo.person.HirePhotoRespVo;

/**
 * 入职人员头像CO
 * @author lvliuhao
 * @version 创建时间：180526 
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/hirePhoto")
public class HirePhotoCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(HirePhotoCO.class);

	@Autowired
	public HirePhotoService hirePhotoService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;
	
	@Autowired
	private BusinessConfig businessConfig;
	

	/**
	 * 获取入职人员头像
	 * @param hireId
	 * @return
	 */
	@GetMapping(value = "/getOne")
	public BaseResponseVO getOne(String rsHireId) {
		logger.info("getHireBankacct param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		PsRsHirePhotoPrimary primary = new PsRsHirePhotoPrimary();
		primary.setPhotoSizename("PAGE");
		primary.setRsHireId(rsHireId);
		PsRsHirePhoto photo = hirePhotoService.getHirePhoto(primary);
		if(photo!=null){
			HirePhotoRespVo respVo = objectMapper.map(photo, HirePhotoRespVo.class);
			respVo.setImgUrl(hirePhotoService.buildEmplPhoto(photo));
			return buildRespone(respVo);
		}
		return buildRespone(BaseRetCode.CODE_ERROR_HIRE_PHOTO_NOT_EXIST);
	}
	
	/**
	 * 保存入职人员头像
	 * @param hireId
	 * @return
	 */
	@PostMapping(value = "/upload")
	public BaseResponseVO upload(@RequestParam("fileName") String base64,PsRsHirePhoto photo) {
		logger.info("upload hire photo param= {}", photo);
		if(StringUtils.isBlank(photo.getRsHireId())){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(StringUtils.isBlank(base64)){
			return buildRespone(BaseRetCode.CODE_ERROR_EMPPHOTO_NOT_ALLOWED);
		}
		byte[] imgByte = File2Byte.base64ToImgByte(base64, businessConfig.getEmplePhotoResourceLocation());
		PsRsHirePhotoPrimary primary = new PsRsHirePhotoPrimary();
		primary.setPhotoSizename("PAGE");
		primary.setRsHireId(photo.getRsHireId());
		PsRsHirePhoto nowPhoto = hirePhotoService.getHirePhoto(primary);
		try {
			if(nowPhoto!=null){
				nowPhoto.setEmployeePhoto(imgByte);
				nowPhoto.setPhotoImgname(photo.getPhotoImgname());
				photo = hirePhotoService.save(nowPhoto);
			}else{
				photo.setEmployeePhoto(imgByte);
				photo.setPhotoImgname(photo.getPhotoImgname());
				photo.setPhotoSizename("PAGE");
				photo = hirePhotoService.save(photo);
			}
		} catch (Exception e) {
			logger.error("upload hire photo fail",e);
			return buildRespone(BaseRetCode.CODE_ERROR_HIREFILE_UPLOAD_FAILED);
		}
		if(photo!=null){
			HirePhotoRespVo respVo = objectMapper.map(photo, HirePhotoRespVo.class);
			respVo.setImgUrl(hirePhotoService.buildEmplPhoto(photo));
			return buildRespone(respVo);
		}
		return buildRespone(BaseRetCode.CODE_ERROR_HIREFILE_UPLOAD_FAILED);
	}
}
